<template>
  <div class="menu-page">
    <div class="header">
      <h1>点餐</h1>
    </div>
    
    <div class="content">
      <!-- 左侧分类 -->
      <div class="categories">
        <div 
          v-for="category in categories" 
          :key="category"
          :class="['category-item', { active: selectedCategory === category }]"
          @click="selectCategory(category)"
        >
          {{ category }}
        </div>
      </div>
      
      <!-- 右侧菜品 -->
      <div class="menu-items">
        <div 
          v-for="item in filteredItems" 
          :key="item.id"
          class="menu-item"
        >
          <div class="item-info">
            <h3>{{ item.name }}</h3>
            <p class="price">¥{{ item.price }}</p>
          </div>
          <button @click="addToCart(item)" class="add-btn">+</button>
        </div>
      </div>
    </div>
    
    <!-- 购物车 -->
    <div class="cart-summary" v-if="cartItemCount > 0">
      <div class="cart-info">
        <span>已选 {{ cartItemCount }} 件商品</span>
        <span class="total">合计: ¥{{ cartTotal }}</span>
      </div>
      <button @click="goToCheckout" class="checkout-btn">去结算</button>
    </div>
  </div>
</template>

<script>
import { computed } from 'vue'
import { useRouter } from 'vue-router'
import { useOrderStore } from '../store/orderStore'

export default {
  name: 'MenuPage',
  setup() {
    const router = useRouter()
    const orderStore = useOrderStore()
    
    const categories = computed(() => orderStore.categories)
    const selectedCategory = computed(() => orderStore.selectedCategory)
    const menuItems = computed(() => orderStore.menuItems)
    const cartItemCount = computed(() => orderStore.cartItemCount)
    const cartTotal = computed(() => orderStore.cartTotal)
    
    const filteredItems = computed(() => {
      return menuItems.value.filter(item => item.category === selectedCategory.value)
    })
    
    const selectCategory = (category) => {
      orderStore.selectCategory(category)
    }
    
    const addToCart = (item) => {
      orderStore.addToCart(item)
    }
    
    const goToCheckout = () => {
      router.push('/checkout')
    }
    
    return {
      categories,
      selectedCategory,
      menuItems,
      cartItemCount,
      cartTotal,
      filteredItems,
      selectCategory,
      addToCart,
      goToCheckout
    }
  }
}
</script>

<style scoped>
.menu-page {
  padding: 20px;
  padding-bottom: 80px;
}

.header h1 {
  color: #333;
  margin-bottom: 20px;
}

.content {
  display: flex;
  height: calc(100vh - 180px);
}

.categories {
  width: 30%;
  border-right: 1px solid #eee;
  padding-right: 10px;
  overflow-y: auto;
}

.category-item {
  padding: 15px 10px;
  text-align: center;
  cursor: pointer;
  border-bottom: 1px solid #eee;
}

.category-item.active {
  background-color: #42b983;
  color: white;
}

.menu-items {
  width: 70%;
  padding-left: 10px;
  overflow-y: auto;
}

.menu-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 15px 0;
  border-bottom: 1px solid #eee;
}

.item-info h3 {
  margin-bottom: 5px;
}

.price {
  color: #42b983;
  font-weight: bold;
}

.add-btn {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  background-color: #42b983;
  color: white;
  border: none;
  font-size: 18px;
  cursor: pointer;
}

.cart-summary {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background-color: white;
  box-shadow: 0 -2px 10px rgba(0,0,0,0.1);
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 15px 20px;
  max-width: 500px;
  margin: 0 auto;
}

.cart-info {
  display: flex;
  flex-direction: column;
}

.total {
  font-weight: bold;
  color: #42b983;
}

.checkout-btn {
  background-color: #42b983;
  color: white;
  border: none;
  padding: 10px 20px;
  border-radius: 4px;
  cursor: pointer;
}
</style>